package com.java.util;


/**
 * 找到数组前三个最大元素
 */
public class FindTopThree {

    public static void main(String[] args) {

        int[] array = {5, 1, 9, 6, 7, 3, 8};
        findTopThreeInOnePass(array);
    }

    public static void findTopThreeInOnePass(int[] array) {

        int first = Integer.MIN_VALUE;
        int second = Integer.MIN_VALUE;
        int third = Integer.MIN_VALUE;

        for (int i : array) {

            if (i > first) {
                third = second;
                second = first;
                first = i;

            } else if (i > second) {
                third = second;
                second = i;

            } else if (i > third) {
                third = i;
            }


        }


        System.out.println("Top 3 elements are: " + first + ", " + second + ", " + third);
    }
}
